package com.hou.solr.solrj;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/product")
public class TestController {

    @Autowired
    private SolrClient solrClient;

    //添加/更新索引,如果索引库中id存在则更新
    @GetMapping("/add")
    public String add() {
        //创建索引对象
        SolrInputDocument document = new SolrInputDocument();
        String id = UUID.randomUUID().toString().replaceAll("_", "");
        document.setField("id", id);
        document.setField("product_catalog_name", "手机饰品");
        document.setField("product_name", "大奶姚牌手机");
        document.setField("product_picture", "yaozhen.png");
        try {
            solrClient.add(document);
            solrClient.commit();//必须提交
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }

    //删除索引
    @GetMapping("/delete")
    public String delete() {
        try {
            //根据id删除
            solrClient.deleteById("8888");
            //删除所有
            //solrClient.deleteByQuery("*:*");
            //如果配置文件中指定了core,这里不能写core名字,直接提交
            solrClient.commit("new_core");
            solrClient.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "删除成功";
    }

    //根据id查询
    @GetMapping("/findById")
    public String find() {
        try {
            SolrDocument document = solrClient.getById("6");
            return document.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "查询出错";
    }

    //综合查询
    @GetMapping("/select")
    public Map<String, Map<String, List<String>>> selectByQuery() throws IOException, SolrServerException {
        SolrQuery query = new SolrQuery();
        //设置查询条件,和后台管理界面参数相对应
        query.set("q", "product_name:手机");
        query.set("fq", "product_price:[* TO 10]");
        query.addSort("product_price", SolrQuery.ORDER.asc);
        //分页
        query.setStart(0);
        query.setRows(20);
        //只查询指定域
        query.set("fl", "id,product_name,product_price");
        //高亮
        //打开开关
        query.setHighlight(true);
        //指定高亮域
        query.addHighlightField("product_name");
        //设置前缀
        query.setHighlightSimplePre("<span style='color:red'>");
        //设置后缀
        query.setHighlightSimplePost("</span>");
        QueryResponse queryResponse = solrClient.query(query);
        SolrDocumentList results = queryResponse.getResults();
        long numFound = results.getNumFound();
        System.out.println("查询到的条数:" + numFound);
        //获取高亮显示的结果, 高亮显示的结果和查询结果是分开放的
        Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting();
        return highlight;
    }


}
